package com.syni.mdd.sjb.community.base.aop;

import com.syni.mdd.sjb.common.component.mybatis.common.entity.DxshAppUserInfo;
import com.syni.mdd.sjb.common.component.mybatis.common.mapper.DxshAppUserInfoMapper;
import com.syni.mdd.sjb.common.component.mybatis.community.entity.SjbQywxAdmin;
import com.syni.mdd.sjb.common.component.utils.common.utils.RequestHeaderData;
import com.syni.mdd.sjb.common.component.utils.common.utils.ResultMapHelper;
import com.syni.mdd.sjb.community.base.annotion.AdminAuthVerification;
import com.syni.mdd.sjb.community.base.auth.BackPcAuthUserBo;
import com.syni.mdd.sjb.community.base.auth.JwtTokenUtils;
import com.syni.mdd.sjb.community.base.constants.ResultCode;
import com.syni.mdd.sjb.community.base.constants.ResultMsg;
import com.syni.mdd.sjb.community.base.entity.vo.BaseRqVo;
import com.syni.mdd.sjb.community.base.service.SjbQywxAdminService;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;

@Aspect
@Order(2)
@Component
public class ControllerAdminBackAuth {
    private final Logger log = LoggerFactory.getLogger(getClass());

    @Autowired
    JwtTokenUtils jwtTokenUtils;

    @Autowired
    SjbQywxAdminService sjbQywxAdminService;

    @Autowired
    DxshAppUserInfoMapper dxshAppUserInfoMapper;

    @Around("@annotation(adminAuthVerification)")
    public Object around(ProceedingJoinPoint proceedingJoinPoint, AdminAuthVerification adminAuthVerification) throws Throwable {
        Map<String,Object> argsMap = new HashMap<>();
        Object[] args = proceedingJoinPoint.getArgs();
        BaseRqVo baseRqVo = null;
        if(args.length > 0){
            for(Object arg : args){
                if(arg instanceof BaseRqVo){
                    baseRqVo = (BaseRqVo) arg;
                }
            }
        }
        RequestHeaderData requestHeaderData = new RequestHeaderData();
        String token = requestHeaderData.getString("qwtoken");
        int checkCode = jwtTokenUtils.checkToken(token);
        if(checkCode != JwtTokenUtils.TOKEN_SUCCESS){
            return ResultMapHelper.result(ResultCode.CODE_PC_NO_LOGIN, ResultMsg.MSG_PC_TOKEN_FAIL);
        }
        if(baseRqVo != null){
            BackPcAuthUserBo backPcAuthUserBo = JwtTokenUtils.backPcAuthUserBoThreadLocal.get();
            baseRqVo.setQxwxAdminId(backPcAuthUserBo.getQxwxAdminId());
            baseRqVo.setQxyxUserid(backPcAuthUserBo.getQxyxUserid());
            baseRqVo.setCorpid(backPcAuthUserBo.getCorpid());
            JwtTokenUtils.backPcAuthUserBoThreadLocal.remove();
            SjbQywxAdmin sjbQywxAdmin = sjbQywxAdminService.getById(baseRqVo.getQxwxAdminId());
            if(sjbQywxAdmin != null && sjbQywxAdmin.getSjbUserId() != null){
                DxshAppUserInfo dxshAppUserInfo = dxshAppUserInfoMapper.selectById(sjbQywxAdmin.getSjbUserId());
                if(dxshAppUserInfo != null){
                    baseRqVo.setUserId(dxshAppUserInfo.getUserId());
                    baseRqVo.setSjbUserId(dxshAppUserInfo.getId());
                }
            }
        }
        return proceedingJoinPoint.proceed(args);
    }
}
